package gorm

import (
	"encoding/json"
	"time"
)

const timeFormat = "2006-01-02 15:04:05"

type Time struct {
	time.Time
}

func (r *Time) UnmarshalJSON(b []byte) error {
	var s string
	if err := json.Unmarshal(b, &s); err != nil {
		return err
	}

	t, err := time.Parse(timeFormat, s)
	if err != nil {
		return err
	}
	r.Time = t

	return nil
}

func (r Time) MarshalJSON() ([]byte, error) {
	return json.Marshal(r.Format(timeFormat))
}
